import { Injectable } from '@angular/core'
import { numScale, compareTwoCurrencies } from 'app/utils/helpers'
import { CurrencyService } from '@shared/services/currency.service'
import { IOption, IFormItem, FormItemType } from '@shared/interfaces/common'
import { ECUPIChannel, ECVISChannel, ECMCCChannel, ECJCBChannel } from '@shared/interfaces/acquirer'
import { Validators } from '@angular/forms'
import { I18NService } from '@core'
import {
  AcquiringIINMaxLength,
  EcAcquiringIINReg,
  ChannelMerchantNameMaxLength,
  TransactionFeeReg,
  ChannelSignPublicKeyIDMaxLength,
  ChannelSignPublicKeyIDReg,
  ChannelEncryptPublicKeyIDMaxLength,
  ChannelEncryptPublicKeyIDReg,
  merNameMaxLength,
  CilEncryptPublicKeyIDMaxLength,
  CilEncryptPublicKeyIDReg,
  CilSignPublicKeyIDMaxLength,
  CilSignPublicKeyIDReg,
} from 'app/utils/contraints'
@Injectable()
export class EcChannelService {
  currencyOptions = []

  constructor(public currencyService: CurrencyService, private i18n: I18NService) {}
  genUPIFormItems = (
    currencyOptions: IOption[],
    channelOptions: IOption[],
    handleChannelChange,
    handleChanMerIDChange,
  ): IFormItem[] => {
    return [
      {
        label: 'Acquiring IIN',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Acquiring IIN' }),
          existed: this.trans('VALIDATION.CODE_EXISTS', { name: 'Acquiring IIN' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: AcquiringIINMaxLength }),
          pattern: this.trans('VALIDATION.PATTERN.NUMBER_ONLY'),
        },
        formControlName: 'acquiringIIN',
        change: handleChanMerIDChange,
      },
      {
        label: 'Transaction Currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Currency' }),
        },
        formControlName: 'currencies',
        type: FormItemType.CURRENCY_SELECT,
        multiple: 'multiple',
        allowClear: true,
        options: currencyOptions,
        compareFn: compareTwoCurrencies,
      },
      {
        label: 'Channel Merchant Name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Channel Merchant Name' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: merNameMaxLength }),
        },
        formControlName: 'chanMerName',
      },
      {
        label: 'Transaction Fee',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Fee' }),
          pattern: this.trans('VALIDATION.PATTERN.FOUR_DECIMAL_MAX_AND_NO_NEGATIVE'),
        },
        errTip: `Transaction Fee ${this.trans('VALIDATION.PATTERN.BETWEEN_ZERO_AND_TEN_PERCENT')}`,
        type: FormItemType.INPUTGROUP,
        addAfter: '%',
        formControlName: 'fee',
        inputType: 'number',
      },
      {
        label: 'Channel Encryption Public Key ID',
        errTips: {
          pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: ChannelEncryptPublicKeyIDMaxLength }),
        },
        formControlName: 'chanEncryptPubKeyId',
      },
      {
        label: 'Channel Encryption Public Key',
        formControlName: 'chanEncryptPubKey',
        type: FormItemType.FILE,
      },
      {
        label: 'Channel Sign Public Key ID',
        errTips: {
          pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: ChannelEncryptPublicKeyIDMaxLength }),
        },
        formControlName: 'chanSignPubKeyId',
      },
      {
        label: 'Channel Sign Public Key',
        formControlName: 'chanSignPubKey',
        type: FormItemType.FILE,
      },
      {
        label: 'CIL Encryption Private Key ID',
        errTips: {
          pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: ChannelEncryptPublicKeyIDMaxLength }),
        },
        formControlName: 'cilEncryPriKeyId',
      },
      {
        label: 'CIL Encryption Private Key',
        formControlName: 'cilEncryPriKey',
        type: FormItemType.FILE,
      },
      {
        label: 'CIL Sign Private Key ID',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'CIL Sign Private Key ID' }),
          pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: ChannelEncryptPublicKeyIDMaxLength }),
        },
        formControlName: 'cilSignPriKeyId',
      },
      {
        label: 'CIL Sign Private Key',
        formControlName: 'cilSignPriKey',
        type: FormItemType.FILE,
      },
    ]
  }


  genKBANKalidateRules = (channel: ECVISChannel): { [key: string]: any } => {
    return {
      id: [null, []],
      chanCode: [channel && channel.chanCode, [Validators.required]],
      chanMerId: [channel && channel.chanMerId, [Validators.required, Validators.maxLength(64)]],
      chanMerName: [
        channel && channel.chanMerName,
        [Validators.required, Validators.maxLength(ChannelMerchantNameMaxLength)],
      ],
      fee: [
        channel && numScale(channel.fee, 2),
        [Validators.required, Validators.max(10), Validators.min(0), Validators.pattern(TransactionFeeReg)],
      ],
      currencies: [channel && channel.currencies, [Validators.required]],
      isLoaded: [channel && channel.isLoaded],
    }
  }

  genUPIValidateRules = (channel: ECUPIChannel): { [key: string]: any } => {
    return {
      id: [null, []],
      chanCode: [channel && channel.chanCode, [Validators.required]],
      acquiringIIN: [
        channel && channel.acquiringIIN,
        [Validators.required, Validators.maxLength(AcquiringIINMaxLength), Validators.pattern(EcAcquiringIINReg)],
      ],
      chanMerName: [
        channel && channel.chanMerName,
        [Validators.required, Validators.maxLength(ChannelMerchantNameMaxLength)],
      ],
      fee: [
        channel && numScale(channel.fee, 2),
        [Validators.required, Validators.max(10), Validators.min(0), Validators.pattern(TransactionFeeReg)],
      ],
      currencies: [channel && channel.currencies, [Validators.required]],
      chanEncryptPubKeyId: [
        channel && channel.chanEncryptPubKeyId,
        [Validators.maxLength(ChannelEncryptPublicKeyIDMaxLength), Validators.pattern(ChannelEncryptPublicKeyIDReg)],
      ],
      chanEncryptPubKey: [channel && channel.chanEncryptPubKey, []],
      chanSignPubKeyId: [
        channel && channel.chanSignPubKeyId,
        [Validators.maxLength(ChannelSignPublicKeyIDMaxLength), Validators.pattern(ChannelSignPublicKeyIDReg)],
      ],
      chanSignPubKey: [channel && channel.chanSignPubKey, []],
      cilEncryPriKeyId: [
        channel && channel.cilEncryPriKeyId,
        [Validators.maxLength(CilEncryptPublicKeyIDMaxLength), Validators.pattern(CilEncryptPublicKeyIDReg)],
      ],
      cilEncryPriKey: [channel && channel.cilEncryPriKey, []],
      cilSignPriKeyId: [
        channel && channel.cilSignPriKeyId,
        [
          Validators.required,
          Validators.maxLength(CilSignPublicKeyIDMaxLength),
          Validators.pattern(CilSignPublicKeyIDReg),
        ],
      ],
      cilSignPriKey: [channel && channel.cilSignPriKey, [Validators.required]],
      isLoaded: [channel && channel.isLoaded],
    }
  }

  genKBankFormItems = (
    currencyOptions: IOption[],
    channelOptions: IOption[],
    handleChannelChange,
    handleMerchantIDChange,
  ): IFormItem[] => {
    return [
      {
        label: 'Merchant ID',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Merchant ID' }),
          existed: this.trans('VALIDATION.CODE_EXISTS', { name: 'Merchant ID' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: 64 }),
          pattern: this.trans('VALIDATION.PATTERN.NUMBER_ONLY'),
        },
        formControlName: 'chanMerId',
        change: handleMerchantIDChange,
      },
      {
        label: 'Transaction Currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Currency' }),
        },
        formControlName: 'currencies',
        type: FormItemType.CURRENCY_SELECT,
        multiple: 'multiple',
        allowClear: true,
        options: currencyOptions,
        compareFn: compareTwoCurrencies,
      },
      {
        label: 'Transaction Fee',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Fee' }),
          pattern: this.trans('VALIDATION.PATTERN.FOUR_DECIMAL_MAX_AND_NO_NEGATIVE'),
        },
        errTip: `Transaction Fee ${this.trans('VALIDATION.PATTERN.BETWEEN_ZERO_AND_TEN_PERCENT')}`,
        type: FormItemType.INPUTGROUP,
        addAfter: '%',
        formControlName: 'fee',
        inputType: 'number',
      },
      {
        label: 'Channel Merchant Name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Channel Merchant Name' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: merNameMaxLength }),
        },
        formControlName: 'chanMerName',
      },
    ]
  }

  genVISFormItems = (
    currencyOptions: IOption[],
    channelOptions: IOption[],
    handleChannelChange,
    handleMerchantIDChange,
  ): IFormItem[] => {
    return [
      {
        label: this.trans('BIZ.ACQUIRER.OTHER.CHANNEL.PAYMENT_CHANNEL'),
        errTips: {
          required: this.trans('VALIDATION.REQUIRED_SELECT', {
            name: this.trans('BIZ.ACQUIRER.OTHER.CHANNEL.PAYMENT_CHANNEL'),
          }),
        },
        formControlName: 'chanCode',
        type: FormItemType.SELECT,
        options: channelOptions,
        compareFn: (o1, o2) => o1 === o2,
        multiple: 'default',
        change: handleChannelChange,
      },
      {
        label: 'Merchant ID',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Merchant ID' }),
          existed: this.trans('VALIDATION.CODE_EXISTS', { name: 'Merchant ID' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: 64 }),
          pattern: this.trans('VALIDATION.PATTERN.NUMBER_ONLY'),
        },
        formControlName: 'chanMerId',
        change: handleMerchantIDChange,
      },
      {
        label: 'Transaction Currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Currency' }),
        },
        formControlName: 'currencies',
        type: FormItemType.CURRENCY_SELECT,
        multiple: 'multiple',
        allowClear: true,
        options: currencyOptions,
        compareFn: compareTwoCurrencies,
      },
      {
        label: 'Transaction Fee',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Fee' }),
          pattern: this.trans('VALIDATION.PATTERN.FOUR_DECIMAL_MAX_AND_NO_NEGATIVE'),
        },
        errTip: `Transaction Fee ${this.trans('VALIDATION.PATTERN.BETWEEN_ZERO_AND_TEN_PERCENT')}`,
        type: FormItemType.INPUTGROUP,
        addAfter: '%',
        formControlName: 'fee',
        inputType: 'number',
      },
      {
        label: 'Channel Merchant Name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Channel Merchant Name' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: merNameMaxLength }),
        },
        formControlName: 'chanMerName',
      },
    ]
  }

  genVISValidateRules = (channel: ECVISChannel): { [key: string]: any } => {
    return {
      id: [null, []],
      chanCode: [channel && channel.chanCode, [Validators.required]],
      paymentBrand: [channel && channel.paymentBrand, [Validators.required]],
      chanMerId: [channel && channel.chanMerId, [Validators.required, Validators.maxLength(64)]],
      chanMerName: [
        channel && channel.chanMerName,
        [Validators.required, Validators.maxLength(ChannelMerchantNameMaxLength)],
      ],
      fee: [
        channel && numScale(channel.fee, 2),
        [Validators.required, Validators.max(10), Validators.min(0), Validators.pattern(TransactionFeeReg)],
      ],
      currencies: [channel && channel.currencies, [Validators.required]],
      isLoaded: [channel && channel.isLoaded],
    }
  }

  genMCCFormItems = (
    currencyOptions: IOption[],
    channelOptions: IOption[],
    handleChannelChange,
    handleMerchantIDChange,
  ): IFormItem[] => {
    return [
      {
        label: this.trans('BIZ.ACQUIRER.OTHER.CHANNEL.PAYMENT_CHANNEL'),
        errTips: {
          required: this.trans('VALIDATION.REQUIRED_SELECT', {
            name: this.trans('BIZ.ACQUIRER.OTHER.CHANNEL.PAYMENT_CHANNEL'),
          }),
        },
        formControlName: 'chanCode',
        type: FormItemType.SELECT,
        options: channelOptions,
        compareFn: (o1, o2) => o1 === o2,
        multiple: 'default',
        change: handleChannelChange,
      },
      {
        label: 'Merchant ID',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Merchant ID' }),
          existed: this.trans('VALIDATION.CODE_EXISTS', { name: 'Merchant ID' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: 64 }),
          pattern: this.trans('VALIDATION.PATTERN.NUMBER_ONLY'),
        },
        formControlName: 'chanMerId',
        change: handleMerchantIDChange,
      },
      {
        label: 'Transaction Currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Currency' }),
        },
        formControlName: 'currencies',
        type: FormItemType.CURRENCY_SELECT,
        multiple: 'multiple',
        allowClear: true,
        options: currencyOptions,
        compareFn: compareTwoCurrencies,
      },
      {
        label: 'Transaction Fee',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Fee' }),
          pattern: this.trans('VALIDATION.PATTERN.FOUR_DECIMAL_MAX_AND_NO_NEGATIVE'),
        },
        errTip: `Transaction Fee ${this.trans('VALIDATION.PATTERN.BETWEEN_ZERO_AND_TEN_PERCENT')}`,
        type: FormItemType.INPUTGROUP,
        addAfter: '%',
        formControlName: 'fee',
        inputType: 'number',
      },
      {
        label: 'Channel Merchant Name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Channel Merchant Name' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: merNameMaxLength }),
        },
        formControlName: 'chanMerName',
      },
    ]
  }

  genMCCValidateRules = (channel: ECVISChannel): { [key: string]: any } => {
    return {
      id: [null, []],
      chanCode: [channel && channel.chanCode, [Validators.required]],
      paymentBrand: [channel && channel.paymentBrand, [Validators.required]],
      chanMerId: [channel && channel.chanMerId, [Validators.required, Validators.maxLength(64)]],
      chanMerName: [
        channel && channel.chanMerName,
        [Validators.required, Validators.maxLength(ChannelMerchantNameMaxLength)],
      ],
      fee: [
        channel && numScale(channel.fee, 2),
        [Validators.required, Validators.max(10), Validators.min(0), Validators.pattern(TransactionFeeReg)],
      ],
      currencies: [channel && channel.currencies, [Validators.required]],
      isLoaded: [channel && channel.isLoaded],
    }
  }

  genJCBFormItems = (
    currencyOptions: IOption[],
    channelOptions: IOption[],
    handleChannelChange,
    handleMerchantIDChange,
  ): IFormItem[] => {
    return [
      {
        label: this.trans('BIZ.ACQUIRER.OTHER.CHANNEL.PAYMENT_CHANNEL'),
        errTips: {
          required: this.trans('VALIDATION.REQUIRED_SELECT', {
            name: this.trans('BIZ.ACQUIRER.OTHER.CHANNEL.PAYMENT_CHANNEL'),
          }),
        },
        formControlName: 'chanCode',
        type: FormItemType.SELECT,
        options: channelOptions,
        compareFn: (o1, o2) => o1 === o2,
        multiple: 'default',
        change: handleChannelChange,
      },
      {
        label: 'Merchant ID',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Merchant ID' }),
          existed: this.trans('VALIDATION.CODE_EXISTS', { name: 'Merchant ID' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: 64 }),
          pattern: this.trans('VALIDATION.PATTERN.NUMBER_ONLY'),
        },
        formControlName: 'chanMerId',
        change: handleMerchantIDChange,
      },
      {
        label: 'Transaction Currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Currency' }),
        },
        formControlName: 'currencies',
        type: FormItemType.CURRENCY_SELECT,
        multiple: 'multiple',
        allowClear: true,
        options: currencyOptions,
        compareFn: compareTwoCurrencies,
      },
      {
        label: 'Transaction Fee',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Fee' }),
          pattern: this.trans('VALIDATION.PATTERN.FOUR_DECIMAL_MAX_AND_NO_NEGATIVE'),
        },
        errTip: `Transaction Fee ${this.trans('VALIDATION.PATTERN.BETWEEN_ZERO_AND_TEN_PERCENT')}`,
        type: FormItemType.INPUTGROUP,
        addAfter: '%',
        formControlName: 'fee',
        inputType: 'number',
      },
      {
        label: 'Channel Merchant Name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Channel Merchant Name' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: merNameMaxLength }),
        },
        formControlName: 'chanMerName',
      },
    ]
  }

  genJCBValidateRules = (channel: ECVISChannel): { [key: string]: any } => {
    return {
      id: [null, []],
      chanCode: [channel && channel.chanCode, [Validators.required]],
      paymentBrand: [channel && channel.paymentBrand, [Validators.required]],
      chanMerId: [channel && channel.chanMerId, [Validators.required, Validators.maxLength(64)]],
      chanMerName: [
        channel && channel.chanMerName,
        [Validators.required, Validators.maxLength(ChannelMerchantNameMaxLength)],
      ],
      fee: [
        channel && numScale(channel.fee, 2),
        [Validators.required, Validators.max(10), Validators.min(0), Validators.pattern(TransactionFeeReg)],
      ],
      currencies: [channel && channel.currencies, [Validators.required]],
      isLoaded: [channel && channel.isLoaded],
    }
  }

  private trans(str: string, params?: Object): string {
    return this.i18n.fanyi(str, params)
  }

  getECChannelFormItems = (channelOptions: IOption[], handleChannelChange): IFormItem[] => {
    return [
      {
        label: this.trans('BIZ.ACQUIRER.OTHER.CHANNEL.PAYMENT_CHANNEL'),
        errTips: {
          required: this.trans('VALIDATION.REQUIRED_SELECT', {
            name: this.trans('BIZ.ACQUIRER.OTHER.CHANNEL.PAYMENT_CHANNEL'),
          }),
        },
        formControlName: 'chanCode',
        type: FormItemType.SELECT,
        options: channelOptions,
        compareFn: (o1, o2) => o1 === o2,
        multiple: 'default',
        change: handleChannelChange,
      },
    ]
  }

  genECChannelValidateRules = (channel: ECVISChannel): { [key: string]: any } => {
    return {
      id: [null, []],
      chanCode: [channel && channel.chanCode, [Validators.required]],
      paymentBrand: [channel && channel.paymentBrand, [Validators.required]],
      isLoaded: [channel && channel.isLoaded],
    }
  }
}
